package proxyc

import (
	"gitee.com/yysf-xin/dproxy/controller/spider"
	"gitee.com/yysf-xin/dproxy/pkg/app"
	"gitee.com/yysf-xin/dproxy/pkg/model"
	"github.com/go-chi/chi/v5"
	"net/http"
)

type ServerControl struct {
	Conf   *app.Config
	V2flay *spider.V2flay
}

func (c *ServerControl) InitRoute(router chi.Router) {
	router.Route("/proxy", func(r chi.Router) {
		r.Get("/enable", app.JsonHttp(nil, c.enableStatus))
		r.Post("/enable", app.JsonHttp(func() interface{} {
			return &model.Enable{}
		}, c.handlerEnable))
	})
	router.Route("/fly",c.V2flay.InitRoute)
}

func (c *ServerControl) handlerEnable(_ http.ResponseWriter, _ *http.Request, i interface{}) (interface{}, error) {
	c.Conf.File.EnableProxy = i.(*model.Enable).Enabled
	return nil, nil
}

func (c *ServerControl) enableStatus(_ http.ResponseWriter, _ *http.Request, _ interface{}) (rs interface{}, err error) {
	rs = c.Conf.File.EnableProxy
	return
}
